package day01;

/*
 * Author: JacksonCoder
 * FileName: bubbleSort
 * Version: 1.0.0
 * Date: 2024/12/26
 */

public class bubbleSort {

    public static void BubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        // 0 ~ N - 1
        // 0 ~ N - 2
        // 0 ~ N - 3
        int N = arr.length;
        for (int end = N - 1; end > 0; end--) {
            // 0 ~ end 处理一些逻辑
            // 0 -> 1 ?    1 -> 2  ...   end - 1 -> end
            for (int second = 1; second <= end; second++) {
                if (arr[second - 1] > arr[second]) {
                    swap(arr, second - 1, second);
                }
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void printArray(int[] arr) {
        for (int a : arr) {
            System.out.print(a + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 4, 2, 0, 9, 7, 1, 6};
        printArray(arr);
        BubbleSort(arr);
        printArray(arr);
    }
}
